package com.bdth.bdthcms.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.bdth.bdthcms.entity.Depart;
import com.bdth.bdthcms.mapper.DepartMapper;
import com.bdth.bdthcms.service.DepartService;
import com.bdth.bdthcms.vo.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author LGP
 * @since 2019-04-02
 */
@Service
public class DepartServiceImpl extends ServiceImpl<DepartMapper, Depart> implements DepartService {

    @Autowired
    private DepartMapper departMapper;

    public Pagination<Depart> pageLists(Pagination<Depart> pagination, Map<String, Object> query) {
        query.put("offset", pagination.offset());
        query.put("limit", pagination.limit());
        query.put("sort", pagination.getSort());
        query.put("order", pagination.getOrder());
        pagination.setItems(baseMapper.list(query));
        pagination.setTotal(baseMapper.count(query));
        return pagination;
    }

    @Override
    public String maxCode(Integer pid) {
        Map<String, Object> query = new HashMap<>();
        query.put("pid",pid);
        return baseMapper.maxCode(query);
    }

    @Override
    public List<Depart> getAllList() {
        return baseMapper.selectAllList();
    }

    private List<Map<String,Object>> getPlist(List<Map<String,Object>> treeList){

        for (Map<String,Object> bean: treeList) {
            List<Map<String,Object>> childlist =  baseMapper.selectPlist((Integer)bean.get("id"));
            if(childlist.isEmpty()){

            } else {
                getPlist(childlist);
                bean.put("status","open");
                bean.put("children", childlist);
            }
        }
        return treeList;
    }

    @Override
    public List<Map<String,Object>> getDepartTree(Map<String, Object> query ){
        List<Map<String, Object>> result = new ArrayList<>();
        if((Integer)query.get("departid") == 0) {
            List<Map<String, Object>> treeList = baseMapper.selectPlist(0);
            List<Map<String, Object>> treeResult = getPlist(treeList);
            Map<String, Object> root = new HashMap<>();
            root.put("id", 0);
            root.put("children", treeResult);
            root.put("status", "open");
            root.put("text", "顶级部门");
            result.add(root);
        }else{
            List<Map<String, Object>> treeList = baseMapper.selectPlist((Integer)query.get("departid"));
            List<Map<String, Object>> treeResult = getPlist(treeList);
            Map<String, Object> root = new HashMap<>();
            Depart depart = baseMapper.selectById((Integer)query.get("departid"));
            root.put("id",(Integer)query.get("departid"));
            root.put("children", treeResult);
            root.put("status", "open");
            root.put("text", depart.getDepartname());
            result.add(root);
            return result;
        }
        return result;
    }

    @Override
    public List<Map<String,Object>> departTree(Map<String, Object> query ){
        List<Map<String, Object>> result = new ArrayList<>();
        if((Integer)query.get("departid") == 0) {
            List<Map<String, Object>> treeList = baseMapper.selectPlist(0);
            List<Map<String, Object>> treeResult = getPlist(treeList);
            Map<String, Object> root = new HashMap<>();
            root.put("id", 0);
            root.put("children", treeResult);
            root.put("status", "open");
            root.put("text", "请选择");
            result.add(root);
        }else{
            List<Map<String, Object>> treeList = baseMapper.selectPlist((Integer)query.get("departid"));
            List<Map<String, Object>> treeResult = getPlist(treeList);
            Map<String, Object> root = new HashMap<>();
            Depart depart = baseMapper.selectById((Integer)query.get("departid"));
            root.put("id",(Integer)query.get("departid"));
            root.put("children", treeResult);
            root.put("status", "open");
            root.put("text", depart.getDepartname());
            result.add(root);
            return result;
        }
        return result;
    }

    @Override
    public String getDepartIds(Integer departid) {
        String result = "";
        Depart depart = departMapper.selectById(departid);
        EntityWrapper<Depart> entityWrapper = new EntityWrapper<>();
        entityWrapper.like("code",depart.getCode());
        List<Depart> list = departMapper.selectList(entityWrapper);
        for(Depart bean : list){
            result+=bean.getId()+",";
        }
        //去掉最后一位逗号
        result = result.substring(0,result.length()-1);
        return result;
    }
}
